package com.itrencai;

import java.awt.*;
import java.awt.event.*;

public class JiaoTong extends Frame {
    boolean redStatus = false, greenStatus = false, yellowStatus = false;
    int j = 0;

    public void paint(Graphics g) {
        Color c = g.getColor();
        if (redStatus == true) {
            j++;
            g.setColor(Color.RED);
            g.fillOval(100, 50, 50, 50);
            g.drawString("红灯亮了" + j + "秒", 100, 120);
        } else {
            g.setColor(Color.BLACK);
            g.fillOval(100, 50, 50, 50);
        }
        if (yellowStatus == true) {
            j++;
            g.setColor(Color.YELLOW);
            g.fillOval(200, 50, 50, 50);
            g.drawString("黄灯注意啦" + j + "秒", 100, 220);
        } else {
            g.setColor(Color.BLACK);
            g.fillOval(200, 50, 50, 50);
        }
        if (greenStatus == true) {
            g.setColor(Color.GREEN);
            g.fillOval(300, 50, 50, 50);
            g.drawString("绿灯行" + j + "秒", 100, 320);
        } else {
            g.setColor(Color.BLACK);
            g.fillOval(300, 50, 50, 50);
        }
        g.setColor(c);
    }

    public void Lauch() {
        setTitle("交通灯指示ʾ");
        setSize(400, 300);
        setBackground(Color.WHITE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        this.setVisible(true);
        new Thread(new PaintThread()).start();
    }

    public static void main(String[] args) {
        JiaoTong a = new JiaoTong();
        a.Lauch();
    }

    public class PaintThread implements Runnable {
        public void run() {
            for (int i = 0; i < 30; i++) {
                switch (i) {
                    case 0:
                        j = 0;
                        redStatus = true;
                        greenStatus = false;
                        yellowStatus = false;
                        break;
                    case 10:
                        j = 0;
                        redStatus = false;
                        greenStatus = false;
                        yellowStatus = true;
                        break;
                    case 13:
                        j = 0;
                        redStatus = false;
                        greenStatus = true;
                        yellowStatus = false;
                        break;
                    default:
                        break;
                }
                repaint();
                if (i == 29) i = -1;
                try {
                    Thread.sleep(1000);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

